﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Jy123.Web.Manage
{
    public partial class ProsType : MgConfig
    {
        private Dictionary<int, Jy123.Model.Public_PropsType> fatherDic = new Dictionary<int, Jy123.Model.Public_PropsType>();
        private List<Jy123.Model.Public_PropsType> listPropsType;
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("PropsType.aspx");
            Jy123.BLL.Public_PropsType BLLStarType = new Jy123.BLL.Public_PropsType();
            listPropsType = BLLStarType.GetModelList(string.Empty);
            foreach (Jy123.Model.Public_PropsType type in listPropsType)
            {
                fatherDic.Add(type.ID, type);
            }

            if (!Page.IsPostBack)
            {
                LoadInitialValue(listPropsType);
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_PropsType propsType = e.Item.DataItem as Jy123.Model.Public_PropsType;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = propsType.ID.ToString();
                Label nameLabel = e.Item.FindControl("propsTypeNameLabel") as Label;
                nameLabel.Text = propsType.PName;
                Label fatherNameLabel = e.Item.FindControl("fatherNameLabel") as Label;
                if (propsType.PFather.HasValue)
                {
                    fatherNameLabel.Text = fatherDic[propsType.PFather.Value].PName;
                }
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_PropsType BLLStarType = new Jy123.BLL.Public_PropsType();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_PropsType propsType = BLLStarType.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                propsTypeNameTextBox.Text = propsType.PName;
                if (propsType.PFather.HasValue)
                {
                    fatherDropDown.SelectedValue = propsType.PFather.Value.ToString();
                }
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLStarType.Delete(id);
                listPropsType = BLLStarType.GetModelList(string.Empty);
                LoadInitialValue(listPropsType);
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_PropsType BLLStarType = new Jy123.BLL.Public_PropsType();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_PropsType propsType = new Jy123.Model.Public_PropsType();
                propsType.ID = BLLStarType.Add(propsType);//为PTree字段，此处先创建一条空记录，再更新此记录
                SetProsTypeValue(propsType);
                BLLStarType.Update(propsType);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_PropsType propsType = BLLStarType.GetModel(id);
                SetProsTypeValue(propsType);
                BLLStarType.Update(propsType);
            }
            listPropsType = BLLStarType.GetModelList(string.Empty);
            LoadInitialValue(listPropsType);
            ClearPage();
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            propsTypeNameTextBox.Text = string.Empty;
        }

        private void SetProsTypeValue(Jy123.Model.Public_PropsType type)
        {
            type.PName = propsTypeNameTextBox.Text;
            int fatherId = int.Parse(fatherDropDown.SelectedValue);
            if (fatherId > 0)
            {
                type.PFather = fatherId;
                Jy123.Model.Public_PropsType fatherType = fatherDic[fatherId];
                type.PTree = fatherType.PTree + type.ID.ToString() + ",";
            }
            else
            {
                type.PFather = null;
                type.PTree = "," + type.ID.ToString() + ",";
            }            
        }

        private void LoadInitialValue(List<Jy123.Model.Public_PropsType> listPropsType)
        {
            propsTypeRepeater.DataSource = listPropsType;
            propsTypeRepeater.DataBind();
            propsTypeRepeater.Visible = listPropsType.Count > 0;
            BindDropDown(listPropsType);
        }

        private void BindDropDown(List<Jy123.Model.Public_PropsType> listPropsType)
        {
            fatherDropDown.Items.Clear();
            fatherDropDown.DataSource = listPropsType;
            fatherDropDown.DataTextField = "PName";
            fatherDropDown.DataValueField = "ID";
            fatherDropDown.DataBind();
            fatherDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
        }
    }
}

